package com.mayisoftware.mayioa.business.human.impl.controller;

import com.mayisoftware.mayioa.business.human.api.domain.HCStatisticInfo;
import com.mayisoftware.mayioa.business.human.api.service.IHCBoardService;
import com.mayisoftware.mayioa.business.statistic.api.domain.EChartsBarVO;
import com.mayisoftware.mayioa.business.statistic.api.domain.EchartsPieVO;
import com.mayisoftware.mayioa.business.statistic.api.domain.SeriseData;
import com.mayisoftware.mayioa.business.statistic.api.domain.SerizeData;
import com.mayisoftware.mayioa.platform.api.domain.SysDictData;
import com.mayisoftware.mayioa.platform.api.service.ISysDictDataService;
import com.mayisoftware.mayioa.platform.impl.web.base.BaseController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * 骨干统计控制器
 */
@Controller
@RequestMapping("/human/board")
public class HCBoardController extends BaseController{
    /**
    * 统计服务
     */
    @Autowired
    private IHCBoardService statisticService;
    @Autowired
    private ISysDictDataService dictService;
    /**
     * 前缀
     */
    private String prefix = "human/board";

    /**
     * 最大显示条数
     */
    private final  static Integer MAX_VIEW =5;

    /**
     * 统计页面
     */
    @RequiresPermissions("human:board:view")
    @GetMapping()
    public String list()
    {
//        List<StatisDept> list= statisticService.selectCountStatisByDeptAndUserType("BU");
//        mmap.put("buTypeList",list);
        return prefix + "/board";
    }

    /**
     * 根据Bu统计饼图
     */
    @RequiresPermissions("human:board:view")
    @PostMapping("/list/listByPie")
    @ResponseBody
    public EchartsPieVO listByPie()
    {
        EchartsPieVO res = new EchartsPieVO();
        List<String> data = new ArrayList<String>();
        List<SerizeData> serizeData = new ArrayList<SerizeData>();

        List<HCStatisticInfo> list= statisticService.getStatisticByCompleteStatus();
        res.setData(data);
        res.setSerizeData(serizeData);

        StringBuilder sb = new StringBuilder();
        for (HCStatisticInfo hcStatisticInfo : list) {
            SerizeData sdata = new SerizeData();
            String complete = dictService.selectDictLabel("recruit_status",hcStatisticInfo.getCompleteStatus());
            sdata.setName(complete);
            sdata.setValue(hcStatisticInfo.getNum());
            serizeData.add(sdata);
            sb.append(complete+":"+hcStatisticInfo.getNum()+"人");
        }
        data.add(sb.toString());

        return res;
    }

    /**
     *
     */
    @RequiresPermissions("human:board:view")
    @PostMapping("/list/listByCheckPdu")
    @ResponseBody
    public EChartsBarVO listByCheckPdu()
    {
        Set<String> checkPduFilter = new HashSet<String>();
        EChartsBarVO eChartsBarVO = new EChartsBarVO();
        List<String> data = new ArrayList<String>();
        List<SeriseData> seriesData = new ArrayList<SeriseData>();
        Set<String> xAixsData = new HashSet<String>();

        List<HCStatisticInfo> list= statisticService.getStatisticByCheckPdu();

        for(HCStatisticInfo sd: list){
            String checkUnit = dictService.selectDictLabel("check_unit",sd.getCheckPdu());
            if(checkUnit==null){
                checkUnit = "未指定考核单元";
            }
            xAixsData.add(checkUnit);
            checkPduFilter.add(sd.getCheckPdu());
        }
        eChartsBarVO.setxAixsData(xAixsData);
        eChartsBarVO.setLegnedData(data);
        eChartsBarVO.setSeriesData(seriesData);

        List<SysDictData> recruitDicts = dictService.selectDictDataByType("recruit_status");


        for (SysDictData dict :recruitDicts) {
            if(dict.getDictValue().equalsIgnoreCase("0")){
                continue;
            }
            SeriseData sData = new SeriseData();
            String complete = dictService.selectDictLabel("recruit_status",dict.getDictValue());
            sData.setName(complete);
            sData.setType("bar");
            sData.setStack("checkPdu");
            List<Integer> ndata = new ArrayList<Integer>();
            for (String checkPdu: checkPduFilter) {
                int num =0;
                for (HCStatisticInfo sd :list) {
                    if ((checkPdu != null && sd.getCheckPdu() != null) &&!checkPdu.equalsIgnoreCase(sd.getCheckPdu())){
                        continue;
                    }
                    if (dict.getDictValue().equalsIgnoreCase(sd.getCompleteStatus())){
                        num = sd.getNum();
                        break;
                    }
                }
                ndata.add(num);
            }
            sData.setData(ndata);

            seriesData.add(sData);
        }
        return eChartsBarVO;
    }

    /**
     *
     */
    @RequiresPermissions("human:board:view")
    @PostMapping("/list/listByRegion")
    @ResponseBody
    public EChartsBarVO listByRegion()
    {
        Set<String> deptFilter = new HashSet<String>();
        EChartsBarVO eChartsBarVO = new EChartsBarVO();
        List<String> data = new ArrayList<String>();
        List<SeriseData> seriesData = new ArrayList<SeriseData>();
        Set<String> xAixsData = new HashSet<String>();

        List<HCStatisticInfo> list= statisticService.getStatisticByCheckPdu();

        for(HCStatisticInfo sd: list){
            String checkUnit = dictService.selectDictLabel("human_pdu",sd.getPdu());
            if(checkUnit==null){
                checkUnit = "未指定PDU";
            }
            xAixsData.add(checkUnit);
        }
        eChartsBarVO.setxAixsData(xAixsData);
        eChartsBarVO.setLegnedData(data);
        eChartsBarVO.setSeriesData(seriesData);

        for(int i=0;i<list.size();i++){
            HCStatisticInfo sd = list.get(i);
            String complete = dictService.selectDictLabel("recruit_status",sd.getCompleteStatus());

//            data.add(sd.getDeptName());
            SeriseData sData = new SeriseData();
            sData.setName(complete);
            sData.setType("bar");
//            sData.setBarWidth("15");
            List<Integer> ndata = new ArrayList<Integer>();
            for(String s :xAixsData){
                ndata.add(sd.getNum());
            }
            sData.setData(ndata);
            seriesData.add(sData);
        }
        return eChartsBarVO;
    }

    /**
     *
     */
    @RequiresPermissions("human:board:view")
    @PostMapping("/list/listByPdu")
    @ResponseBody
    public EChartsBarVO listByPdu()
    {
        Set<String> deptFilter = new HashSet<String>();
        EChartsBarVO eChartsBarVO = new EChartsBarVO();
        List<String> data = new ArrayList<String>();
        List<SeriseData> seriesData = new ArrayList<SeriseData>();
        Set<String> xAixsData = new HashSet<String>();

        List<HCStatisticInfo> list= statisticService.getStatisticByCheckPdu();

        for(HCStatisticInfo sd: list){
            String checkUnit = dictService.selectDictLabel("check_region",sd.getCheckPdu());
            if(checkUnit==null){
                checkUnit = "未指定地域";
            }
            xAixsData.add(checkUnit);
        }
        eChartsBarVO.setxAixsData(xAixsData);
        eChartsBarVO.setLegnedData(data);
        eChartsBarVO.setSeriesData(seriesData);

        for(int i=0;i<list.size();i++){
            HCStatisticInfo sd = list.get(i);
            String complete = dictService.selectDictLabel("recruit_status",sd.getCompleteStatus());

//            data.add(sd.getDeptName());
            SeriseData sData = new SeriseData();
            sData.setName(complete);
            sData.setType("bar");
//            sData.setBarWidth("15");
            List<Integer> ndata = new ArrayList<Integer>();
            for(String s :xAixsData){
                ndata.add(sd.getNum());
            }
            sData.setData(ndata);
            seriesData.add(sData);
        }
        return eChartsBarVO;
    }
}
